package cn.itcast.eduservice.controller;


import cn.itcast.baseservice.exceptionhandle.GuliException;
import cn.itcast.commonutils.R;
import cn.itcast.eduservice.entity.EduTeacher;
import cn.itcast.eduservice.entity.vo.TeacherQuery;
import cn.itcast.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Duanzihuang
 * @since 2020-04-27
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("list")
    public R findAll() {
        List<EduTeacher> list = eduTeacherService.list(null);

        return R.ok().data("items",list);
    }

    @ApiOperation(value = "根据Id查询讲师")
    @GetMapping("{id}")
    public R findTeacherById(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);

        return R.ok().data("item",eduTeacher);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R deleteTeacherById(@ApiParam(name = "id",value = "讲师ID",required = true) @PathVariable String id){
        boolean result = eduTeacherService.removeById(id);

        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "分页查询讲师信息")
    @GetMapping("list/{current}/{limit}")
    public R findTeachers(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable long current,
                          @ApiParam(name = "limit",value = "每页多少条",required = true) @PathVariable long limit){

        Page<EduTeacher> page = new Page<>(current,limit);

        eduTeacherService.page(page, null);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @ApiOperation(value = "分页并且带条件查询讲师信息")
    @PostMapping("list/condition/{current}/{limit}")
    public R findTeachers(@ApiParam(name = "current",value = "当前页",required = true) @PathVariable long current,
                          @ApiParam(name = "limit",value = "每页多少条",required = true) @PathVariable long limit,
                          @ApiParam(required = false) @RequestBody TeacherQuery teacherQuery){

        Page<EduTeacher> page = new Page<>(current,limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

//        try {
//            int a = 10 / 0;
//        } catch (Exception e) {
//            throw new GuliException(400,"自定义异常，被除数不能为0...");
//        }
        
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }

        if (!StringUtils.isEmpty(level)){
            queryWrapper.eq("level",level);
        }

        if (!StringUtils.isEmpty(begin)){
            queryWrapper.ge("gmt_create",begin);
        }

        if (!StringUtils.isEmpty(end)){
            queryWrapper.le("gmt_create",end);
        }

        // 按照添加时间倒序排序
        queryWrapper.orderByDesc("gmt_create");

        eduTeacherService.page(page, queryWrapper);

        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("")
    public R addTeacher(@ApiParam(required = true) @RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("{id}")
    public R updateTeacher(@PathVariable String id,
                           @ApiParam(required = true) @RequestBody EduTeacher eduTeacher){
        eduTeacher.setId(id);
        boolean flag = eduTeacherService.updateById(eduTeacher);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

